package cs633.action;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

import cs633.util.DBManager;
import cs633.util.Photo;
import cs633.util.Utils;

public class UserPhotosAction extends ActionSupport implements ServletRequestAware {
	private static final long serialVersionUID = -7756367675266105763L;
	private HttpServletRequest request;

	
	public String deletePhoto() {
		HttpSession session = request.getSession(false);
		String photoIndex = request.getParameter("photoTitle");
		if (photoIndex == null || photoIndex.trim().length() == 0)
			photoIndex = "0";
		int index = Integer.parseInt(photoIndex);
		
		String albumName = (String) session.getAttribute("albumName");
		String userId = (String) session.getAttribute("userID");
		
		List<Photo> aList = (List<Photo>) session.getAttribute("photos");		
		Photo photo = aList.get(index);
		long photoId = photo.getPhotoId();
		
		Connection con = null;
		
		try {
			con = DBManager.getInstance().getConn();
			String sql = "DELETE FROM USER_IMG WHERE USER_IMG_ID = ?";			
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setLong(1, photoId);						
			int count = pstmt.executeUpdate();
			if (count > 0) {
				ResourceBundle rb = ResourceBundle.getBundle("resource");
				String s3 = rb.getString("s3");
				String filePath = s3.concat(File.separator).concat("images").concat(File.separator);				
				String delLoc = filePath.concat(photo.getAlbumLoc()).concat(File.separator).concat(photo.getPhotoName());
				File delFile = new File(delLoc);
				delFile.delete();
				
				session.setAttribute("photos", Utils.getPhotos(userId, albumName));
			}
			
		} catch (Exception e) {
			System.out.println("Error: " + e.getMessage());
		} finally {
			try {
				con.close();
			} catch (Exception e) {
				
			}
		}		
		
		return SUCCESS;
	}
	public String updatePhoto() {
		HttpSession session = request.getSession(false);
		String photoIndex = request.getParameter("photoId");
		if (photoIndex == null || photoIndex.trim().length() == 0)
			photoIndex = "0";
		int index = Integer.parseInt(photoIndex);
		
		List<Photo> aList = (List<Photo>) session.getAttribute("photos");		
		Photo photo = aList.get(index);
		long photoId = photo.getPhotoId();
		
		String title = request.getParameter("updatePhotoTitle");
		String desc = request.getParameter("updatePhotoDesc");
		String albumName = (String) session.getAttribute("albumName");
		String userId = (String) session.getAttribute("userID");
		
		Connection con = null;
		
		try {
			con = DBManager.getInstance().getConn();
			String sql = "UPDATE USER_IMG SET IMG_DESC = ?, IMG_TITLE = ? WHERE USER_IMG_ID = ?";
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, desc);
			pstmt.setString(2, title);
			pstmt.setLong(3, photoId);
			
			int count = pstmt.executeUpdate();						
			session.setAttribute("photos", Utils.getPhotos(userId, albumName));
			
		} catch (Exception e) {
			System.out.println("Error: " + e.getMessage());
		} finally {
			try {
				con.close();
			} catch (Exception e) {
				
			}
		}
		
		return SUCCESS;
	}	
	public String userPhotos() {
		String userId = request.getParameter("userId");
		String albumName = request.getParameter("albumName");
		HttpSession session = request.getSession(false);
		List<Photo> aList = Utils.getPhotos(userId, albumName);
		session.setAttribute("photos", aList);
		session.setAttribute("albumName", albumName);

		return SUCCESS;
	}
	
	public String publicPhotos() {
		String albumName = request.getParameter("albumName");
		HttpSession session = request.getSession(false);
		List<Photo> aList = Utils.getPublicPhotos(albumName);
		session.setAttribute("photos", aList);
		session.setAttribute("albumName", albumName);
		
		return SUCCESS;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}

}
